// merchants/recordsByUser/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CurrentPage: 1,
    loadingMoreHidden: 1,
    recordList: [],
    isJifen: 0,
    clientuid: 0,
    useruid: 0,
    user: true,
    creatuid: 0,
    cid: 0
  },

  getCardRecordList: function(uid, cid, isJifen){
    let that = this;
    let param = {};
    let clientuid = that.data.clientuid;
    let useruid = that.data.useruid;
    if (isJifen == 1) {
      if (clientuid > 0) {
        useruid = clientuid
      } else {
        useruid = app.globalData.userInfo.Uid;
      }
      param = {
        createUid: uid,
        uid: useruid,
        page: that.data.CurrentPage
      }
    } else {
      useruid = app.globalData.userInfo.Uid;
      param = {
        createUid: uid,
        uid: useruid,
        cid: cid,
        page: that.data.CurrentPage
      }
    }
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardRecordList',
      data:param,
      method: 'POST',
      success: function(res){
        if(res.data.Code==0){
          let recordList = res.data.Data.List;
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.recordList
            recordList = preGoods.concat(recordList);
          }
          for(let i = 0; i < recordList.length; i++){
            recordList[i].AddTime = recordList[i].AddTime.substr(0, 10);
          }
          that.setData({
            recordList: recordList,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.jifen==1){
      wx.setNavigationBarTitle({
        title: '积分记录'
      })
    }else{
      wx.setNavigationBarTitle({
        title: '消费记录'
      })
    }
    this.setData({
      isJifen: options.jifen
    })
    if(options.clientuid){
      this.setData({
        clientuid: options.clientuid
      })
    }
    if (options.user){
      this.setData({
        user: options.user
      })
    }
    if (options.cid) {
      this.setData({
        cid: options.cid,
        creatuid: options.uid
      })
    }
    this.getCardRecordList(options.uid, options.cid, options.jifen);
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 加载更多
  loadMore: function (e) {
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.CurrentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      that.getCardRecordList(that.data.creatuid, that.data.cid, that.data.isJifen);
    } else {
      return
    }
  },

})